const app = getApp(), fetchApi = require("../../utils/httpPost.js"), ad = require("../../template/ad/adTemplate.js"),
commonJs = require("../../component/common.js");
import Toast from '../../component/vant/toast/toast';
const CryptoJS = require("../../utils/aesUtil.js")
Page({
  data: {
    imgurl: app.getImgUrl(),
    userInfo: {},
    hasUserInfo: false,
    is_mer: 0,
    is_admin: 0,
    orderItemArr: [],
    adBanner:[],
    hasBanner:false,
    shop_type: 0,
    isLogin:0,
    phone_num:'',
    isBandPhone:0,
    property_no_pay:0,
    is_show_mer:0,
    is_show_repair:0,
    isiphonex: app.globalData.isiphonex,
    isxiaomi: app.globalData.isxiaomi,
    repair_count:0,
    estate_user_count:0,
    is_test_user:0,
    nick_name:'',
    face_url:'',
    user_no:'',
    menuList:[],//菜单列表
    approveCountCheck:0,
  },
  //事件处理函数
  bindViewTap: function() {    
    var isLogin = app.getStorageSync("isLogin");
    if(isLogin){
      wx.navigateTo({ url: '/pages/account/index' })
    }else{
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
  },
  onLoad: function () {
    // this.doLogin()
  },
  goLogin:function(){
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  doLogin: function(){
    var that = this
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log("code", res.code)
        fetchApi.httpPost('/user/wxLogin', { code: res.code }, function (res) {
          console.log("登录返回数据", res)
          if(res.data.code == 0){
            // that.onHttpGetUserInfo()
          }
          that.setData({ isLoading: false, loading: true })
        }, function (res) { 
          that.setData({ isLoading: false, loading: true })
        })
      },fail(res){
        that.setData({ isLoading: false, loading: true })
      }
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/user/getUserInfoData', {
    }, function (res) {
      console.log("getUserInfoData 数据", res)
      if (res.data.code == 0) {
        that.setData({ 
          orderItemArr: res.data.data.orderItemArr,
          phone_num: res.data.data.phone_num!=null&&res.data.data.phone_num.length > 0 ? res.data.data.phone_num :'请绑定手机号',
          isBandPhone:res.data.data.phone_num!=null&&res.data.data.phone_num.length > 0?1:0,
          property_no_pay: res.data.data.property_no_pay,
          is_mer: res.data.data.is_mer,
          is_admin: res.data.data.is_admin,
          is_show_mer:res.data.data.is_show_mer,
          is_test_user: res.data.data.is_test_user,
          is_show_repair:res.data.data.is_show_repair,
          repair_count:res.data.data.repair_count,
          estate_user_count:res.data.data.estate_user_count,
          nick_name:res.data.data.nick_name, 
          face_url :res.data.data.face_url,
          user_no :res.data.data.user_no,
          menuList:res.data.data.menuList,
          approveCountCheck: res.data.data.approveCountCheck,
        })
        if(res.data.data.phone_num!=null && res.data.data.phone_num.length > 0){
          app.setStorageSync("phone_num", res.data.data.phone_num);
        }
        if (!that.data.hasBanner) {
          that.setData({
            adBanner: res.data.data.banner,
            hasBanner: res.data.data.hasBanner
          })
        }
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  onShow: function () {
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    var is_mer = app.getStorageSync("is_mer");
    var is_admin = app.getStorageSync("is_admin");
    that.setData({
      isLogin: isLogin,
      is_mer: is_mer,
      is_admin: is_admin
    })
    app.globalData.userInfo = app.getStorageSync("userInfo");
    console.log('isLogin', isLogin)
    console.log('app.globalData.userInfo', app.globalData.userInfo)
    if (isLogin) {
      that.setData({
        userInfo: app.globalData.userInfo
      })
      that.onHttpInit()
    }
    app.getCurrentTabbar(3,this);
  },
  shop_order:function(e){
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  callPlatform(e){
    wx.makePhoneCall({
      phoneNumber: '4006860633',
    })
  },
})
